import { lazy } from 'react'
import { useNavigate } from 'react-router-dom'
/* -------------------------------------------------------- Sync Function -- */
import Logout from '@com/login/logout'
import ClearCache from '@com/login/clearCache'
const Dropdown = lazy(()=>import('@antd/dropdown'))
/**
 * 用户操作控制组件
 * @return {Component} 
 */
const Index = ({ api='user/logout' }) => {
	const navigate = useNavigate( )
	
	const onLogout = ()=> Logout({ api, navigate })
	
	const onClear = () => ClearCache()
	
	const user = window.$fn.getUser()
	
	const onClick = ({ value, label }) => {
		window.$http.put(null, 'project/enabled', { id:value }).then(async ()=>{
			window.$fn.msg.success(`已切换到【${label}】`)
			window.refreshRouter()
		})
	}
	
	return (
		<div className='ex pr20 f12 fxmr'>
			{
				user?.name && <div>欢迎 <span className='c0'>{user?.name}</span></div>
			}
			<span className='cp g6 hover tap db pl10' onClick={onLogout}>退出登录</span>
			<span className='cp g6 hover tap db plr10' onClick={onClear}>刷新缓存</span>
			<Dropdown label='切换系统' ghost={1} cacheApi='project/dict' onClick={onClick} />
		</div>
	)
}
export default Index